package kr.hkit.androidproject_caloriecheck.diary;

import java.util.ArrayList;

import kr.hkit.androidproject_caloriecheck.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	private ArrayList<DayInfo> mDayList;
	private Context mContext;
	private int mResource;
	private LayoutInflater mLiInflater;

	public CalendarAdapter(Context context, int textResource,
			ArrayList<DayInfo> dayList) {
		this.mContext = context;
		this.mDayList = dayList;
		this.mResource = textResource;
		this.mLiInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return mDayList.size();
	}

	public Object getItem(int position) {
		return mDayList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		DayInfo day = mDayList.get(position);

		DayViewHolde dayViewHolder;

		if (convertView == null) {
			convertView = mLiInflater.inflate(mResource, null);

			if (position % 7 == 6) {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP() + getRestCellWidthDP(),
						getCellHeightDP()));
			} else {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP(), getCellHeightDP()));
			}

			dayViewHolder = new DayViewHolde();

			dayViewHolder.llBackground = (LinearLayout) convertView
					.findViewById(R.id.day_cell_ll_background);
			dayViewHolder.tvDay = (TextView) convertView
					.findViewById(R.id.day_cell_tv_day);

			convertView.setTag(dayViewHolder);
		} else {
			dayViewHolder = (DayViewHolde) convertView.getTag();
		}

		if (day != null) {
			dayViewHolder.tvDay.setText(day.getDay());

			if (day.isInMonth()) {
				if (position % 7 == 0) {
					dayViewHolder.tvDay.setTextColor(Color.RED);
				} else if (position % 7 == 6) {
					dayViewHolder.tvDay.setTextColor(Color.BLUE);
				} else {
					dayViewHolder.tvDay.setTextColor(Color.BLACK);
				}
			} else {
				dayViewHolder.tvDay.setTextColor(Color.GRAY);
			}
		}
		return convertView;

	}
	
	public class DayViewHolde
    {
        public LinearLayout llBackground;
        public TextView tvDay; 
    }
 
    private int getCellWidthDP()
    {
//      int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int cellWidth = 480/7;
         
        return cellWidth;
    }
     
    private int getRestCellWidthDP()
    {
//      int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int cellWidth = 480%7;
         
        return cellWidth;
    }
     
    private int getCellHeightDP()
    {
//      int height = mContext.getResources().getDisplayMetrics().widthPixels;
        int cellHeight = 480/6;
         
        return cellHeight;
    }
}
